
* INSTALL NECESSARY PACKAGES:

* ssc install estout
* ssc install ivreg2
* ssc install ranktest

capture cd "~/Replication/data"
use "Zimbabwe AB all coding.dta", clear
capture mkdir "../tables"


quietly {
	
	merge m:1 districtpc using "zanu territorial control.dta"
	drop if _merge == 2

	drop district_n-provnamefu wardareasq _merge
	duplicates drop round respno, force
	
	merge m:1 zimwardsid using "ward centroids with distances.dta"
	drop if _merge == 2
	drop _merge
	
	merge m:1 zimwardsid using "ward level geo variables.dta"
	drop if _merge == 2
	drop _merge

	
	merge m:1 zimwardsid using "ward to communal lands.dta"
	drop if _merge == 2
	drop _merge
	
	****************************
	***   Create variables   ***
	****************************


	quietly do "../do files/0. abm levels index variables.do"

	egen round_group = group(round)

	* control

	gen zanu_control = zanu_pres
	replace zanu_control = 0 if contestation != 0 & zanu_pres != .
	replace zanu_control = 0 if district == "Umzingwane"
	
	* Posts

	gen post = 0 if year_birth != .
	replace post = 1 if year_birth >= 1980 & year_birth != .
	
	* Ward-level controls

	gen dist_km_moz = log(dist_to_moz)
	gen dist_km_harare = log(dist_to_harare)
	gen area = log(wardareasq)
	gen pop_log = log(pop)
	
	label var dist_km_moz "Dist to Mozambique (log km)"
}



**************************
***   Variable lists   ***
**************************

* Controls for main specifications and robustness checks

global controls post i.round_group shona ndebele other surveyor_gov surveyor_pol dist_km_harare pop_log area

global controls2 post i.round_group surveyor_gov surveyor_pol dist_km_harare pop_log area

global controls3 post i.round_group shona ndebele other surveyor_gov surveyor_pol dist_km_harare pop_log share_wetland share_bare area

global controls4 post i.round_group i.year_birth shona ndebele other surveyor_gov surveyor_pol dist_km_harare pop_log share_wetland share_bare area

* Outcomes to make tables

global table1 formal_politics protest
global table2 no_restrictions free_speak free_vote
global table3 trust_gov trust_zanu trust_local_gov
global table4 consume_politics int_pub_aff contact gov_accountable reject_auth supp_comp


* Geography variables (pre-teatment)

global geographic share_natpark share_water share_bare share_lake share_crop share_forest share_shrub share_grassland share_sp_vegetation share_wetland


****************************
*** Confirm first stage  ***
****************************

reg zanu_control c.dist_km_moz $controls if native == 1, vce(cluster districtpc)



********************************************************************************
***   MAIN TEXT
********************************************************************************

capture erase "../tables/zimbabwe ward_level.tex"

est clear

label var zanu_control "ZANU Control"


quietly foreach macro in table1 table2 table3 table4 {
	est clear
	foreach var of varlist $`macro' {
		
		noisily di `"`: var label `var''"'
		egen `var'_std = std(`var')
		
		eststo: ivreg2 `var'_std (c.zanu_control zanu_control = c.dist_km_moz dist_km_moz) $controls ///
		if native == 1, ///
		partial($controls) cluster(districtpc) ffirst
		
		estadd scalar fstat =e(widstat)
		summarize `var'_std if e(sample)
		noisily lincom zanu_control
		
		drop `var'_std
		
	}


	esttab ///
	using "../tables/zimbabwe ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) title("`macro'" (IV) \label{tab:`macro'}) ///
	style(tex) label booktabs append

	est clear
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		
		eststo: reg `var'_std zanu_control $controls if native == 1, ///
		vce(cluster districtpc)
		noisily lincom zanu_control
		
		drop `var'_std
		
	}

	esttab ///
	using "../tables/zimbabwe ward_level.tex", b(3) se(3) ar(3) ///
	stats(N r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) title("`macro'" (OLS) \label{tab:`macro'}) ///
	style(tex) label booktabs append

}


********************************************************************************
***   APPENDIX
********************************************************************************

capture erase "../tables/zimbabwe appendix ward_level.tex"


*************************************
*** Geographic balance (Table A2) ***
*************************************

est clear
quietly foreach var of varlist $geographic {
	
	noisily di `"`: var label `var''"'
	egen `var'_std = std(`var')
	eststo: reg `var'_std dist_km_moz $controls ///
	if native == 1 & zanu_control != ., ///
	vce(cluster districtpc)
	noisily lincom dist_km_moz
	summarize `var' if e(sample)
	drop `var'_std
}

esttab est1 est2 est3 est4 est5  ///
using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
stats(N r2, fmt(0 3 3 3 3 "")  ///
labels(`"Observations"' `"R$^2$"')) ///
star(* 0.1 ** 0.05 *** 0.01) ///
mtitles(`var') ///
keep(dist_km_moz) title("Balance On Pre-war Geography (OLS)" \label{tab:balance2}) ///
style(tex) booktabs label append

esttab est6 est7 est8 est9 est10 ///
using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
stats(N r2, fmt(0 3 3 3 3 "")  ///
labels(`"Observations"' `"R$^2$"')) ///
star(* 0.1 ** 0.05 *** 0.01) ///
mtitles(`var') ///
keep(dist_km_moz) title("Balance On Pre-war Geography (OLS)" \label{tab:balance2}) ///
style(tex) booktabs label append



**********************************
*** Cohort effects (Table A4)  ***
**********************************

quietly foreach macro in table1 table2 {
	est clear
	foreach var of varlist $`macro' {
		
		noisily di `"`: var label `var''"'
		egen `var'_std = std(`var')
		
		eststo: ivreg2 `var'_std (zanu_control c.zanu_control#c.post = dist_km_moz c.dist_km_moz#c.post) $controls ///
		if native == 1, ///
		partial($controls) cluster(districtpc) ffirst
		
		estadd scalar fstat =e(widstat)
		summarize `var'_std if e(sample)
		noisily lincom zanu_control
		noisily lincom c.zanu_control#c.post
		
		drop `var'_std
		
	}

	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control c.zanu_control#c.post) title("`macro'" (IV) \label{tab:`macro'}) ///
	style(tex) label booktabs append

	est clear
	
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		
		eststo: reg `var'_std zanu_control c.zanu_control#c.post post $controls if native == 1, ///
		vce(cluster districtpc)
		noisily lincom zanu_control
		noisily lincom c.zanu_control#c.post
		
		drop `var'_std
		
	}
	
	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control c.zanu_control#c.post) title("`macro'" (OLS) \label{tab:`macro'}) ///
	style(tex) label booktabs append

}



*************************************************************
***   Robustness checks (Section A.11, Tables A5-Table A8)
*************************************************************

***************************************
*   Robustness (No ethnicities)
***************************************


quietly foreach macro in table1 table2 table3 table4 {
	est clear
	foreach var of varlist $`macro' {
		
		noisily di `"`: var label `var''"'
		egen `var'_std = std(`var')
		eststo: ivreg2 `var' (zanu_control = c.dist_km_moz) $controls2 ///
		if native == 1, ///
		partial($controls2) cluster(districtpc) ffirst
		
		estadd scalar fstat =e(widstat)
		summarize `var' if e(sample)
		drop `var'_std
	}
	
	
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		eststo: reg `var'_std zanu_control $controls2 if native == 1, ///
		vce(cluster districtpc)
		lincom zanu_control
		drop `var'_std
		
	}

	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) ///
	title("`macro' (no ethnicity controls, OLS)" \label{tab:`macro'olsnoeth}) ///
	style(tex) booktabs label append

}



********************************************************
*   Vote Choice (Imbalanced geographic controls)
********************************************************


quietly foreach macro in table1 table2 table3 table4 {
	
	est clear
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		noisily di `"`: var label `var''"'
		eststo: ivreg2 `var'_std (zanu_control = c.dist_km_moz) $controls3 ///
		if native == 1, partial($controls3) cluster(districtpc) ffirst
		
		estadd scalar fstat =e(widstat)
		noisily lincom zanu_control
		summarize `var' if e(sample)
		drop `var'_std
		
	}
	
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		eststo: reg `var'_std zanu_control $controls3 if native == 1, ///
		vce(cluster districtpc)
		noisily lincom zanu_control
		drop `var'_std
	}
	
	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) ///
	title("`macro' (imbalanced geo controls, OLS)" \label{tab:`macro'olsimb}) ///
	style(tex) booktabs label append

}



**********************************************************
*   Vote Choice (full controls with birth year FE)
**********************************************************


quietly foreach macro in table1 table2 table3 table4 {
	
	est clear
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		noisily di `"`: var label `var''"'
		eststo: ivreg2 `var'_std (zanu_control = c.dist_km_moz) $controls4 ///
		if native == 1, partial($controls4) cluster(districtpc) ffirst
		
		estadd scalar fstat =e(widstat)
		noisily lincom zanu_control
		summarize `var' if e(sample)
		drop `var'_std
		
	}
	
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		eststo: reg `var'_std zanu_control $controls4 if native == 1, ///
		vce(cluster districtpc)
		noisily lincom zanu_control
		drop `var'_std
	}
	
	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) ///
	title("`macro' (full controls with birth year FE)" \label{tab:`macro'olsbyfe}) ///
	style(tex) booktabs label append

}


********************************
*   ZANU presence subset
********************************


quietly foreach macro in table1 table2 table3 table4 {
	
	est clear
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		noisily di `"`: var label `var''"'
		eststo: ivreg2 `var'_std (zanu_control = dist_km_moz) $controls ///
		if native == 1 & zanu_pres > 0, ///
		partial($controls) cluster(districtpc) ffirst
		
		estadd scalar fstat =e(widstat)
		lincom zanu_control
		summarize `var' if e(sample)
		drop `var'_std
	}

	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		eststo: reg `var'_std zanu_control $controls ///
		if native == 1 & zanu_pres > 0, vce(cluster districtpc)
		lincom zanu_control
		drop `var'_std
		
	}
	
	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) ///
	title("`macro' (ZANU presence subset, OLS)" \label{tab:`macro'olspressub}) ///
	style(tex) booktabs label append

}


****************************
*   Drop near Harare
****************************


quietly foreach macro in table1 table2 table3 table4 {
	est clear
	foreach var of varlist $`macro' {
		
		noisily di `"`: var label `var''"'
		egen `var'_std = std(`var')
		eststo: ivreg2 `var' (zanu_control = c.dist_km_moz) $controls ///
		if native == 1 & dist_to_harare > 50 & dist_to_harare != ., ///
		partial($controls) cluster(districtpc) ffirst
		
		noisily lincom zanu_control
		estadd scalar fstat =e(widstat)
		summarize `var' if e(sample)
		drop `var'_std
	}
	
	
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		eststo: reg `var'_std zanu_control $controls ///
		if native == 1 & dist_to_harare > 50 & dist_to_harare != ., ///
		vce(cluster districtpc)
		noisily lincom zanu_control
		drop `var'_std
		
	}

	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) ///
	title("`macro' (dropping Harare < 50km)" \label{tab:`macro'olspressub}) ///
	style(tex) booktabs label append

	
}


****************************************************
***   Pre-war institutional strength (Table A9)  ***
****************************************************

est clear

global prewar register

quietly foreach macro in prewar {
	
	est clear
	foreach var of varlist $`macro' {
		
		noisily di `"`: var label `var''"'
		eststo: ivreg2 `var' (zanu_control = c.dist_km_moz) /// 
		$controls if native == 1 & year_birth <= 1979 & year_birth != ., ///
		partial($controls) cluster(districtpc) ffirst
		
		estadd scalar fstat =e(widstat)
		noisily lincom zanu_control
		summarize `var' if e(sample)
		
	}

	foreach var of varlist $`macro' {
		
		eststo: reg `var' zanu_control $controls ///
		if native == 1 & year_birth <= 1979 & year_birth != ., vce(cluster districtpc)
		noisily lincom zanu_control
		
	}

	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) ///
	title("`macro'" \label{tab:`macro'register}) ///
	style(tex) booktabs label append

}

**********************************************
***   Public goods provision (Table A10)   ***
**********************************************

alpha school transport electricity piped_water sewage cell_service health_clinic market_stalls pavement, gen(pub_goods)
global goods pub_goods school transport electricity piped_water sewage cell_service health_clinic market_stalls pavement


quietly foreach macro in goods {
	est clear
	foreach var of varlist $`macro' {
		
		noisily di `"`: var label `var''"'
		egen `var'_std = std(`var')
		
		eststo: ivreg2 `var'_std (c.zanu_control zanu_control = c.dist_km_moz dist_km_moz) $controls ///
		if native == 1, ///
		partial($controls) cluster(districtpc) ffirst
		
		estadd scalar fstat =e(widstat)
		summarize `var'_std if e(sample)
		noisily lincom zanu_control
		
		drop `var'_std
		
	}
	

	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N fstat, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"F-statistic"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) title("`macro'" (IV) \label{tab:`macro'}) ///
	style(tex) label booktabs append

	est clear
	foreach var of varlist $`macro' {
		
		egen `var'_std = std(`var')
		
		eststo: reg `var'_std zanu_control $controls if native == 1, ///
		vce(cluster districtpc)
		noisily lincom zanu_control
		
		drop `var'_std
		
	}

	esttab ///
	using "../tables/zimbabwe appendix ward_level.tex", b(3) se(3) ar(3) ///
	stats(N r2, fmt(0 3 3 3 3 "")  ///
	labels(`"Observations"' `"R$^2$"')) ///
	star(* 0.1 ** 0.05 *** 0.01) ///
	mtitles(`var') ///
	keep(zanu_control) title("`macro'" (OLS) \label{tab:`macro'}) ///
	style(tex) label booktabs append

}


*********************************************
***   Descriptive statistics (Table A3)   ***
*********************************************

est clear

keep if native == 1

estpost summ zanu_control dist_km_moz $table1 $table2 $table3 $table4 surveyor_gov surveyor_pol dist_km_harare area post shona ndebele other $geographic 

esttab using "../tables/zimbabwe sumstats ward_level.tex", replace cells("count mean(fmt(3)) sd(fmt(3)) min max") label nomtitle noobs nonumber


